package com.pan.config;

import com.pan.interceptors.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan({"com.pan.controller"})
public class InterceptorConfig implements WebMvcConfigurer {
    @Autowired
    private Interceptor interceptor;

    //拦截器，将用户信息放入threadLocal

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.interceptor).addPathPatterns("/**").excludePathPatterns("/api/login").
                excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui.html/**",
                        "/swagger-ui/**","/mgr","/mgr/**","/h5","/h5/**","/druid","/druid/**");
        WebMvcConfigurer.super.addInterceptors(registry);
        //.excludePathPatterns("/login","/register","/**/*.html","/**/*,js","/**/*.css");
        //"/login","/register",

        //super.addInterceptors(registry);

    }

    /**
     * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。 需要重新指定静态资源
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        //super.addResourceHandlers(registry);
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

}
